package com.dolphin.aiagentplatform.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dolphin.aiagentplatform.ai.config.ChatStorageConfig;
import com.dolphin.aiagentplatform.mapper.UserLatestChatMapper;
import com.dolphin.aiagentplatform.model.entity.UserLatestChat;
import com.dolphin.aiagentplatform.service.UserLatestChatService;
import com.dolphin.aiagentplatform.utils.ChatIdGenerator;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户最近聊天服务实现类
 */
@Service
@Slf4j
public class UserLatestChatServiceImpl extends ServiceImpl<UserLatestChatMapper, UserLatestChat>
        implements UserLatestChatService {

    @Resource
    private ChatIdGenerator chatIdGenerator;

    @Resource
    private ChatStorageConfig chatStorageConfig;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateLatestChatId(Long userId, String chatId, String agentType) {
        try {
            // 查询是否已存在记录
            UserLatestChat latestChat = lambdaQuery()
                    .eq(UserLatestChat::getUserId, userId)
                    .eq(UserLatestChat::getAgentType, agentType)
                    .eq(UserLatestChat::getIsDelete, 0)
                    .one();

            if (latestChat != null) {
                // 已存在，更新chatId
                UserLatestChat updateChat = new UserLatestChat();
                updateChat.setId(latestChat.getId());
                updateChat.setChatId(chatId);
                return updateById(updateChat);
            } else {
                // 不存在，创建新记录
                UserLatestChat newLatestChat = new UserLatestChat();
                newLatestChat.setUserId(userId);
                newLatestChat.setChatId(chatId);
                newLatestChat.setAgentType(agentType);
                return save(newLatestChat);
            }
        } catch (Exception e) {
            log.error("更新用户最近聊天ID异常", e);
            return false;
        }
    }

    @Override
    public String getLatestChatId(Long userId, String agentType) {
        try {
            // 查询最近的聊天记录
            UserLatestChat latestChat = lambdaQuery()
                    .eq(UserLatestChat::getUserId, userId)
                    .eq(UserLatestChat::getAgentType, agentType)
                    .eq(UserLatestChat::getIsDelete, 0)
                    .one();

            return latestChat != null ? latestChat.getChatId() : null;
        } catch (Exception e) {
            log.error("获取用户最近聊天ID异常", e);
            return null;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteLatestChat(Long userId, String agentType) {
        try {
            // 逻辑删除记录
            return lambdaUpdate()
                    .eq(UserLatestChat::getUserId, userId)
                    .eq(UserLatestChat::getAgentType, agentType)
                    .set(UserLatestChat::getIsDelete, 1)
                    .update();
        } catch (Exception e) {
            log.error("删除用户最近聊天记录异常", e);
            return false;
        }
    }
}